package com.bgds.sys.module.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bgds.sys.common.pagevo.FileResult;
import com.bgds.sys.common.pagevo.PageVo;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.FileuploadUtil;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.module.entity.DeviceAccuracyMange;
import com.bgds.sys.module.entity.dto.DeviceAccuracyManageDto;
import com.bgds.sys.module.service.IDeviceAccuracyMangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  设备精度管理项目前端控制器
 * </p>
 *
 * @author lj
 * @since 2020-02-18
 */
@RestController
@Api(tags = "设备精度管理项目")
@RequestMapping("/deviceAccuracyMange")
@Slf4j
public class DeviceAccuracyMangeController {
    @Autowired
    private IDeviceAccuracyMangeService iDeviceAccuracyMangeService;
    @Autowired
    private FileuploadUtil fileuploadUtil;
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "添加/修改设备精度管理项目",notes = "添加/修改设备精度管理项目",httpMethod = "POST")

    public Result<Object> addOrUpdate(@RequestBody DeviceAccuracyManageDto deviceAccuracyManageDto){

        return iDeviceAccuracyMangeService.saveUpdate(deviceAccuracyManageDto);

    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation(value = "上传图片",notes = "上传图片",httpMethod = "POST")
    public Result<Object> uploadFile(MultipartFile file){
        //上传设备图片
        FileResult fr = fileuploadUtil.saveImage(file, null, "jpg,png");
        if(fr!=null){
            return new ResultUtil<Object>().setData(fr.getServerPath());
        }
        return  new ResultUtil<>().setErrorMsg("上传失败");

    }
    @RequestMapping(value = "/listByConditionPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取设备精度管理项目",notes = "分页获取设备精度管理项目",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", dataType = "String", value = "项目名称")
    })
    public Result<IPage<DeviceAccuracyMange>> listByConditionPage(@ModelAttribute PageVo page, String name){
        IPage<DeviceAccuracyMange> deviceAccuracyMangePage= iDeviceAccuracyMangeService.listByConditionPage(page,name);
        return new ResultUtil<IPage<DeviceAccuracyMange>>().setData(deviceAccuracyMangePage);
    }
    @RequestMapping(value = "/getAllDeviceAccuracyMange", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有设备精度管理项目名称",notes = "获取所有设备精度管理项目名称",httpMethod = "GET",response = Result.class)

    public Result<List<String>> getAllDeviceAccuracyMange(){
        List<String> names= iDeviceAccuracyMangeService.getAllDeviceAccuracyName();
        List<String> filterList= names .stream().distinct().collect(Collectors.toList());
        return new ResultUtil<List<String>>().setData(filterList);
    }

}

